/*
 * @Author: xiaosihan 
 * @Date: 2022-09-27 15:35:00 
 * @Last Modified by: xiaosihan
 * @Last Modified time: 2023-10-22 17:02:19
 */
import { observable } from "mobx";
import { LangugeType } from "./type";

// 获取浏览器默认的语言
function getDefaultLang() {
    const e = (navigator.language || "").toLocaleLowerCase();
    if (/^es/.test(e))
        return LangugeType.ES;
    switch (e) {
        case "ja":
            return LangugeType.JP;
        case "zh-cn":
        case "zh":
            return LangugeType.CN;
        case "zh-tw":
            return LangugeType.TW;
        case "en":
        case "en-us":
        case "es":
            return LangugeType.ES;
        case "pt":
        case "pt-br":
        case "pt-pt":
            return LangugeType.PT;
        case "fr":
        case "fr-fr":
        case "fr-ca":
        case "fr-ch":
            return LangugeType.FR;
        case "de":
        case "de-at":
        case "de-de":
        case "de-li":
        case "de-ch":
            return LangugeType.DE;
        case "ru":
            return LangugeType.RU;
        default:
            return LangugeType.CN
    }
}

const languageStore = observable({

    lang: getDefaultLang() as LangugeType,
    setLang(name: LangugeType) {
        this.lang = name;
    },

});

export default languageStore;